package cn.xfdzx.android.apps.shop.adapter;

import android.app.Dialog;
import android.content.Context;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.activity.XFDGoodsDeatailsActivity;
import cn.xfdzx.android.apps.shop.activity.XFDShopDetailsActivity;
import cn.xfdzx.android.apps.shop.bean.ShopCarListBeen;
import cn.xfdzx.android.apps.shop.contract.ItemClickListener;
import cn.xfdzx.android.apps.shop.presenter.ShopCarListPresenter;
import cn.xfdzx.android.apps.shop.util.StringUtil;
import cn.xfdzx.android.apps.shop.util.Utils;

/**
 * @author gzk
 * @date 2020/4/13 0013
 * description：购物车 批发 中嵌套的商品
 */
public class CarWholesaleGoodsAdapter extends RecyclerView.Adapter<CarWholesaleGoodsAdapter.MyViewHolder> {

    private ItemClickListener mItemClickListener;
    private List<ShopCarListBeen.DataBean.ValidBean.GoodsBeanX> mList;
    private Context mContext;
    private LayoutInflater mLayoutInflater;
    private boolean is_invalid;//是否失效
    private CarWholesaleAdapter adapter;
    private ShopCarListPresenter mPpresenter;
    private int one_position;
    private Dialog dialog;


    public CarWholesaleGoodsAdapter(Context context, List<ShopCarListBeen.DataBean.ValidBean.GoodsBeanX> list, boolean is_invalid, CarWholesaleAdapter adapter, ShopCarListPresenter mPpresenter, int one_position) {
        this.mList = list;
        this.is_invalid = is_invalid;
        this.adapter = adapter;
        this.mContext = context;
        this.mPpresenter = mPpresenter;
        this.one_position = one_position;
        mLayoutInflater = LayoutInflater.from(mContext);
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = mLayoutInflater.inflate(R.layout.item_car_wholesale_goods, null, false);

        MyViewHolder myHolder = new MyViewHolder(inflate);

        return myHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull final MyViewHolder holder, final int position) {

        RoundedCorners roundedCorners = new RoundedCorners(20);
        //通过RequestOptions扩展功能,override:采样率,因为ImageView就这么大,可以压缩图片,降低内存消耗
        RequestOptions options = RequestOptions.bitmapTransform(roundedCorners).override(200, 200);
        options.placeholder(R.drawable.icon_defult)//图片加载出来前，显示的图片
                .fallback(R.drawable.icon_defult) //url为空的时候,显示的图片
                .error(R.drawable.icon_defult);
        Glide.with(mContext).load(mList.get(position).getProductInfo().getImage()).apply(options.transform(new CenterCrop(), roundedCorners)).into(holder.ivShopCarGood);

        holder.tvShopGoodsName.setText(mList.get(position).getProductInfo().getStore_name());

        holder.tvSpecifications.setText("包装规格：" + mList.get(position).getProductInfo().getUnit_name());

        holder.tv_maozhong.setText("毛重: " + mList.get(position).getGoods_weight() + "斤");
        holder.tvPrice.setText("¥" + mList.get(position).getProductInfo().getPrice() + "/" + mList.get(position).getProductInfo().getUnit_name());
        holder.tv_aboutweight.setText("约¥" + StringUtil.initAboutPrice(mList.get(position).getProductInfo().getPrice(), mList.get(position).getGoods_weight()) + "/斤");


        holder.tvNum.setText(mList.get(position).getCart_num() + "");
        holder.tvNum.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mList.get(position).setIs_select(true);
                adapter.total();
                mPpresenter.setNum(one_position, position);

            }
        });
        if (mList.get(position).getCart_num() >= mList.get(position).getProductInfo().getStock()) {
            holder.tvYuNum.setVisibility(View.VISIBLE);
            if (mList.get(position).getStore().getIs_self_store().equals("1")) {//自营
                holder.tvYuNum.setText("库存数仅" + mList.get(position).getProductInfo().getStock() + "斤");
            } else {
                holder.tvYuNum.setText("库存数仅" + mList.get(position).getProductInfo().getStock() + "" + mList.get(position).getProductInfo().getUnit_name());
            }

        } else {
            holder.tvYuNum.setVisibility(View.GONE);
        }

        if (is_invalid) {
            holder.ivShopSelect.setVisibility(View.INVISIBLE);
            holder.llNum.setVisibility(View.INVISIBLE);
            holder.tvImageHint.setVisibility(View.VISIBLE);
            holder.viewImageBg.setVisibility(View.VISIBLE);
            holder.rlGoodsTitle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                }
            });
            holder.rlGoodsTitle.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    return false;
                }
            });
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    return false;
                }
            });
        } else {
            holder.ivShopSelect.setVisibility(View.VISIBLE);
            holder.llNum.setVisibility(View.VISIBLE);
            holder.tvImageHint.setVisibility(View.GONE);
            holder.viewImageBg.setVisibility(View.GONE);
            holder.tvAdd.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (Utils.isFastClick()) {
                        mList.get(position).setIs_select(true);
                        adapter.total();
                        if (mList.get(position).getStore().getIs_self_store().equals("1")) {
                            if (mList.get(position).getCart_num() >= mList.get(position).getIs_support()) {
                                mPpresenter.updateCarNum(mList.get(position).getId() + "", mList.get(position).getCart_num() + 1, one_position, position);
                            } else {
                                mPpresenter.updateCarNum(mList.get(position).getId() + "", mList.get(position).getCart_num() + mList.get(position).getIs_support(), one_position, position);
                            }
                        } else {
                            mPpresenter.updateCarNum(mList.get(position).getId() + "", mList.get(position).getCart_num() + 1, one_position, position);

                        }

                    }
//                    mList.get(position).setCart_num(mList.get(position).getCart_num() + 1);
//                    holder.tvNum.setText(mList.get(position).getCart_num() + "");
                }
            });
            holder.tvJian.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (Utils.isFastClick()) {
                        mList.get(position).setIs_select(true);
                        adapter.total();
                        if (mList.get(position).getStore().getIs_self_store().equals("1")) {
                            if (mList.get(position).getCart_num() > mList.get(position).getIs_support()) {
                                mPpresenter.updateCarNum(mList.get(position).getId() + "", mList.get(position).getCart_num() - 1, one_position, position);
//                        mList.get(position).setCart_num(mList.get(position).getCart_num() - 1);
//                        holder.tvNum.setText(mList.get(position).getCart_num() + "");
                            } else {
                                dialog = Utils.addDialog(mContext, R.layout.dialog, R.string.are_you_sure_you_want_to_delete_this_item, R.string.confirm, new View.OnClickListener() {
                                    @Override
                                    public void onClick(View view) {
                                        if (Utils.isFastClick()) {
                                            mPpresenter.removeGood(mList.get(position).getId() + "", one_position, position, false);
                                            dialog.dismiss();
                                        }
                                    }
                                });
                                dialog.show();
                            }
                        } else {
                            if (mList.get(position).getCart_num() > 1) {
                                mPpresenter.updateCarNum(mList.get(position).getId() + "", mList.get(position).getCart_num() - 1, one_position, position);
//                        mList.get(position).setCart_num(mList.get(position).getCart_num() - 1);
//                        holder.tvNum.setText(mList.get(position).getCart_num() + "");
                            } else {
                                dialog = Utils.addDialog(mContext, R.layout.dialog, R.string.are_you_sure_you_want_to_delete_this_item, R.string.confirm, new View.OnClickListener() {
                                    @Override
                                    public void onClick(View view) {
                                        if (Utils.isFastClick()) {
                                            mPpresenter.removeGood(mList.get(position).getId() + "", one_position, position, false);
                                            dialog.dismiss();
                                        }
                                    }
                                });
                                dialog.show();
                            }
                        }
                    }
                }
            });
            holder.rlGoodsTitle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (Utils.isFastClick()) {
                        Intent intent = new Intent(mContext, XFDGoodsDeatailsActivity.class);
                        intent.putExtra("good_id", mList.get(position).getProductInfo().getId() + "");
                        mContext.startActivity(intent);
                    }
                }
            });
            if (mList.get(position).isIs_select()) {
                holder.ivShopSelect.setImageResource(R.drawable.icon_green_dui);
            } else {
                holder.ivShopSelect.setImageResource(R.drawable.d99999_oval);
            }
            holder.ivShopSelect.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    if (mList.get(position).isIs_select()) {
                        mList.get(position).setIs_select(false);
                        holder.ivShopSelect.setImageResource(R.drawable.d99999_oval);
                    } else {
                        mList.get(position).setIs_select(true);
                        holder.ivShopSelect.setImageResource(R.drawable.icon_green_dui);
                    }
                    adapter.total();
                }
            });
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    dialog = Utils.addDialog(mContext, R.layout.dialog, R.string.are_you_sure_you_want_to_delete_this_item, R.string.confirm, new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            if (Utils.isFastClick()) {
                                mPpresenter.removeGood(mList.get(position).getId() + "", one_position, position, false);
                                dialog.dismiss();
                            }
                        }
                    });
                    dialog.show();
                    return true;
                }
            });
            holder.rlGoodsTitle.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    dialog = Utils.addDialog(mContext, R.layout.dialog, R.string.are_you_sure_you_want_to_delete_this_item, R.string.confirm, new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            if (Utils.isFastClick()) {
                                mPpresenter.removeGood(mList.get(position).getId() + "", one_position, position, false);
                                dialog.dismiss();
                            }
                        }
                    });
                    dialog.show();
                    return true;
                }
            });
        }

    }


    @Override
    public int getItemCount() {
        return mList.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.iv_shop_select)
        ImageView ivShopSelect;
        @BindView(R.id.iv_shop_car_good)
        ImageView ivShopCarGood;
        @BindView(R.id.tv_shop_goods_name)
        TextView tvShopGoodsName;
        @BindView(R.id.tv_specifications)
        TextView tvSpecifications;
        @BindView(R.id.tv_price)
        TextView tvPrice;
        @BindView(R.id.tv_jian)
        TextView tvJian;
        @BindView(R.id.tv_num)
        TextView tvNum;
        @BindView(R.id.tv_add)
        TextView tvAdd;
        @BindView(R.id.ll_num)
        LinearLayout llNum;
        @BindView(R.id.rl_goods_title)
        RelativeLayout rlGoodsTitle;
        @BindView(R.id.tv_image_hint)
        TextView tvImageHint;
        @BindView(R.id.view_image_bg)
        View viewImageBg;
        @BindView(R.id.tv_yu_num)
        TextView tvYuNum;
        @BindView(R.id.tv_aboutweight)
        TextView tv_aboutweight;
        @BindView(R.id.tv_maozhong)
        TextView tv_maozhong;

        public MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

        }
    }


    public void setOnItemClickListener(ItemClickListener itemClickListener) {
        this.mItemClickListener = itemClickListener;

    }

}
